package com.zsyh.jiujiu.fragment.orderFragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.bean.MyOrderBean;
import com.zsyh.jiujiu.conference.chat.ChatActivity;
import com.zsyh.jiujiu.home.OrderInfoActivitty;
import com.zsyh.jiujiu.home.QuernOrderActtivity;
import com.zsyh.jiujiu.home.WuliuActtivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.utils.BigDecimalUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DdaiquerenFragment extends BaseFragment {

    @BindView(R.id.alllist)
    RecyclerView alllist;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    int pageNumber = 1;
    String pageSize = "10";
    String userId;
    String type="1";

    private BaseQuickAdapter<MyOrderBean.DataBean, BaseViewHolder> mAdapter;
    List<MyOrderBean.DataBean> list = new ArrayList<>();

    private BaseQuickAdapter<MyOrderBean.DataBean.JjyhOrderGoodsListBean, BaseViewHolder> mGoodAdapter;


    public static DdaiquerenFragment newInstance(Context contexts, int status) {
        Bundle args = new Bundle();
        args.putInt("status", status);
        DdaiquerenFragment fragment = new DdaiquerenFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_allorder;
    }

    @Override
    protected void initView(View rootView) {
        EventBus.getDefault().register(this);
        userId=(String) SPUtils.get(getActivity(), "userid", "");
        refresh.setEnableRefresh(true);
        refresh.setEnableAutoLoadMore(true);//启用加载
        mAdapter = new BaseQuickAdapter<MyOrderBean.DataBean, BaseViewHolder>(R.layout.item_allorder, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MyOrderBean.DataBean item) {
                helper.setText(R.id.ordercode,"订单号："+item.getOrderNo());
                if(item.getOrderType().equals("0")){
                    if(item.getOrderInstatus().equals("0")){
                        helper.setText(R.id.orderstate,"待回复");
                        helper.setText(R.id.bntright,"提醒回复");
                    }else  if(item.getOrderInstatus().equals("1")){
                        helper.setText(R.id.orderstate,"待确认");
                        helper.setText(R.id.bntright,"确认");
                    }else  if(item.getOrderInstatus().equals("2")){
                        helper.setText(R.id.orderstate,"待发货");
                        //0未确认 1未缴纳，2已缴纳 -1已退回
                        if(item.getChangeInstatus().equals("0")){
                            helper.setText(R.id.bntright,"支付保证金");
                        }else if(item.getChangeInstatus().equals("1")){
                            helper.setText(R.id.bntright,"支付保证金");
                        }else if(item.getChangeInstatus().equals("2")){
                            helper.setVisible(R.id.bnttleft,false);
                            if(EmptyUtil.isEmpty(item.getChangeIntracknamecode())){
                                helper.setText(R.id.bntright,"填写物流");
                            }else{
                                helper.setVisible(R.id.bntright,false);
                            }

                        }
                    }else  if(item.getOrderInstatus().equals("3")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setText(R.id.bntright,"已收货");
                        helper.setText(R.id.orderstate,"待收货");
                    }else  if(item.getOrderInstatus().equals("4")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setVisible(R.id.bntright,false);
                        helper.setText(R.id.orderstate,"已完成");
                    }else  if(item.getOrderInstatus().equals("5")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setVisible(R.id.bntright,false);
                        helper.setText(R.id.orderstate,"关闭");
                    }else  if(item.getOrderInstatus().equals("6")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setVisible(R.id.bntright,false);
                        helper.setText(R.id.orderstate,"已收货");
                    }
                }else if(item.getOrderType().equals("1")){
                    if(item.getOrderOutstatus().equals("0")){
                        helper.setText(R.id.orderstate,"待回复");
                        helper.setText(R.id.bntright,"提醒回复");
                    }else  if(item.getOrderOutstatus().equals("1")){
                        helper.setText(R.id.orderstate,"待确认");
                        helper.setText(R.id.bntright,"确认");
                    }else  if(item.getOrderOutstatus().equals("2")){
                        helper.setText(R.id.orderstate,"待发货");
                        if(item.getChangeOutstatus().equals("0")){
                            helper.setText(R.id.bntright,"支付保证金");
                        }else if(item.getChangeOutstatus().equals("1")){
                            helper.setText(R.id.bntright,"支付保证金");
                        }else if(item.getChangeOutstatus().equals("2")){
                            helper.setVisible(R.id.bnttleft,false);
                            if(EmptyUtil.isEmpty(item.getChangeOuttracknamecode())){
                                helper.setText(R.id.bntright,"填写物流");
                            }else{
                                helper.setVisible(R.id.bntright,false);
                            }

                        }
                    }else  if(item.getOrderOutstatus().equals("3")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setText(R.id.bntright,"已收货");
                        helper.setText(R.id.orderstate,"待收货");
                    }else  if(item.getOrderOutstatus().equals("4")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setVisible(R.id.bntright,false);
                        helper.setText(R.id.orderstate,"已完成");
                    }else  if(item.getOrderOutstatus().equals("5")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setVisible(R.id.bntright,false);
                        helper.setText(R.id.orderstate,"关闭");
                    }else  if(item.getOrderOutstatus().equals("6")){
                        helper.setVisible(R.id.bnttleft,false);
                        helper.setVisible(R.id.bntright,false);
                        helper.setText(R.id.orderstate,"已收货");
                    }

                }
                helper.setOnClickListener(R.id.bntright, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(item.getOrderType().equals("0")){
                            if(item.getOrderInstatus().equals("0")){
                                //IM
                                ActivityUtils.startActivity(ChatActivity.class);
                            }else  if(item.getOrderInstatus().equals("1")){
                                //确认
                                Bundle bundle = new Bundle();
                                bundle.putString("id", item.getId());
                                ActivityUtils.startActivity(bundle, QuernOrderActtivity.class);
                            }else  if(item.getOrderInstatus().equals("2")){

                                if(item.getChangeInstatus().equals("0")){
                                    //支付
                                }else if(item.getChangeInstatus().equals("1")){
                                    //支付
                                }else if(item.getChangeInstatus().equals("2")){
                                    helper.setVisible(R.id.bnttleft,false);
                                    if(EmptyUtil.isEmpty(item.getChangeIntracknamecode())){
                                        Bundle bundle = new Bundle();
                                        bundle.putString("id", item.getId());
                                        ActivityUtils.startActivity(bundle, WuliuActtivity.class);
                                    }else{
                                        helper.setVisible(R.id.bntright,false);
                                    }

                                }
                            }else  if(item.getOrderInstatus().equals("3")){

                            }else  if(item.getOrderInstatus().equals("4")){

                            }else  if(item.getOrderInstatus().equals("5")){

                            }else  if(item.getOrderInstatus().equals("6")){

                            }
                        }else if(item.getOrderType().equals("1")){
                            if(item.getOrderOutstatus().equals("0")){
                                //IM
                                ActivityUtils.startActivity(ChatActivity.class);
                            }else  if(item.getOrderOutstatus().equals("1")){
                                //确认
                                Bundle bundle = new Bundle();
                                bundle.putString("id", item.getId());
                                ActivityUtils.startActivity(bundle,QuernOrderActtivity.class);
                            }else  if(item.getOrderOutstatus().equals("2")){

                                if(item.getChangeOutstatus().equals("0")){
                                    //支付
                                }else if(item.getChangeOutstatus().equals("1")){
                                    //支付
                                }else if(item.getChangeOutstatus().equals("2")) {
                                    helper.setVisible(R.id.bnttleft, false);
                                    if (EmptyUtil.isEmpty(item.getChangeOuttracknamecode())) {
                                        //物流
                                        Bundle bundle = new Bundle();
                                        bundle.putString("id", item.getId());
                                        ActivityUtils.startActivity(bundle, WuliuActtivity.class);
                                    } else {
                                        helper.setVisible(R.id.bntright, false);
                                    }
                                }

                            }else  if(item.getOrderOutstatus().equals("3")){

                            }else  if(item.getOrderOutstatus().equals("4")){

                            }else  if(item.getOrderOutstatus().equals("5")){

                            }else  if(item.getOrderOutstatus().equals("6")){

                            }

                        }
                    }
                });

                helper.setOnClickListener(R.id.bnttleft, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        delete(userId,item.getId());
                    }
                });

                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", item.getId());
                        bundle.putString("data", item.toString());
                        ActivityUtils.startActivity(bundle, OrderInfoActivitty.class);
                    }
                });

                mGoodAdapter = new BaseQuickAdapter<MyOrderBean.DataBean.JjyhOrderGoodsListBean, BaseViewHolder>(R.layout.item_good, item.getJjyhOrderGoodsList()) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder helper, MyOrderBean.DataBean.JjyhOrderGoodsListBean items) {
                        helper.setText(R.id.name,items.getGoodsTitle());
                        helper.setText(R.id.money,items.getPrice()+"");
                        helper.setText(R.id.tottle,"共"+items.getNum()+"件商品  总价值：¥"+ BigDecimalUtil.decimalTwo(items.getPrice()*items.getNum()));
                        if(!EmptyUtil.isEmpty(items.getGoodsPic())){
                            String[] split =items.getGoodsPic().split("\\|");
                            Glide.with(getActivity()).load(split[0]).into(helper.<ImageView>getView(R.id.logo));
                        }


                        if("0".equals(items.getGoodsType())){
                            helper.setImageResource(R.id.huan,R.mipmap.ic_ru);
                            //换入
                        }else if("1".equals(items.getGoodsType())){
                            //换出
                            helper.setImageResource(R.id.huan,R.mipmap.ic_chu);
                        }
                        helper.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Bundle bundle = new Bundle();
                                bundle.putString("id", item.getId());
                                bundle.putString("data", item.toString());
                                ActivityUtils.startActivity(bundle, OrderInfoActivitty.class);
                            }
                        });
                    }
                };
                helper.<RecyclerView>getView(R.id.goodlist).setLayoutManager(new LinearLayoutManager(getActivity()));
                helper.<RecyclerView>getView(R.id.goodlist).setAdapter(mGoodAdapter);
            }
        };
        alllist.setLayoutManager(new LinearLayoutManager(getActivity()));
        alllist.setAdapter(mAdapter);

        MyOrdeer(pageNumber+"",pageSize,userId,type);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNumber=1;
                MyOrdeer(pageNumber+"",pageSize,userId,type);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNumber=pageNumber+1;
                MyOrdeer(pageNumber+"",pageSize,userId,type);
            }
        });
    }

    @Override
    protected void initOnlyFirstLazy() {
        super.initOnlyFirstLazy();
        refreshdata();

    }

    @Override
    protected void initData() {



    }

    public void refreshdata(){
        pageNumber=1;
        MyOrdeer(pageNumber+"",pageSize,userId,type);
    }

    public void loadmore(){

    }

    public void MyOrdeer(String pageNumber,
                         String pageSize,
                         String userId,
                         String type) {

        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getOrderList(pageNumber,
                        pageSize,
                        userId,
                        type)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), true) {
                    @Override
                    protected void onError(String errorMsg) {
                        if(pageNumber.equals("1")){
                            if(list!=null){
                                list.clear();
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                        refresh.finishRefresh();
                        refresh.finishLoadMore();
                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("myorder", JsonStr);
                        Gson gson=new Gson();
                        if(pageNumber.equals("1")){
                            if(list!=null){
                                list.clear();
                            }
                        }
                        MyOrderBean goodBeans=gson.fromJson(JsonStr, MyOrderBean.class);
                        for(int i=0;i<goodBeans.getData().size();i++){
                            list.add(goodBeans.getData().get(i));
                        }

                        mAdapter.notifyDataSetChanged();
                        refresh.finishRefresh();
                        refresh.finishLoadMore();
                    }
                });

    }

    /**
     * 删除
     */
    public void delete(String userid,String oredeid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .delOrder(userid,oredeid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        refreshdata();
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvn messageInfo) {
        if (messageInfo.getMessage().equals("all")) {
            refreshdata();

        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
